/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
//作者:AKPower
//算法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 10;
ll ee[maxn][2], fa[maxn];
map<ll, ll> mp;
ll ls;
struct node
{
    ll x, y, val;
} e[maxn];
void init()
{

    for (ll i = 1; i <= 100100; i++)
        fa[i] = i;
    return;
}

bool cmd(node a, node b)
{
    if (a.val == b.val)
    {
        if (a.x == b.x)
            return a.y < b.y;
        else
            return a.x < b.x;
    }
    return a.val < b.val;
}

ll find(ll x)
{
    return x == fa[x] ? x : (fa[x] = find(fa[x]));
}

int main()
{
    IOS;
    init();
    ls = 1;
    ll n, m;
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll x, y, val;
        cin >> x >> y >> val;
        if (mp.find(x) == mp.end())
        {
            mp[x] = ls++;
        }
        e[i].x = mp[x];
        if (mp.find(y) == mp.end())
        {
            mp[y] = ls++;
        }
        e[i].y = mp[y];
        e[i].val = val;
    }
    ll id, cnt;
    ll ans = 0;
    cin >> id;
    ll tx = find(e[id].x);
    ll ty = find(e[id].y);
    if (tx != ty)
    {
        ans = e[id].val;
        cnt = 1;
    }
    if (tx < ty)
    {
        fa[ty] = tx;
    }
    else
        fa[tx] = ty;
    sort(e + 1, e + 1 + m, cmd);
    for (ll i = 1; i <= m; i++)
    {
        ll tx = find(e[i].x);
        ll ty = find(e[i].y);
        if (tx == ty)
            continue;
        if (tx < ty)
        {
            fa[ty] = tx;
        }
        else
            fa[tx] = ty;
        ans += e[i].val;
        cnt++;
    }
    cout << ans << endl;
    getchar();
    getchar();
    return 0;
}
